package com.group2.view;
import static model.netBase.IP;
import javax.swing.JPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import model.*;
public class BankPayFrame extends JPanel {
	private JTable feetable;
	private static Account MyAccount=null;
	private static String studentId=null;

	/**
	 * Create the panel.
	 */
	public BankPayFrame(String id,Account mine) {
		studentId=id;
		MyAccount=mine;
		setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		
		JLabel lblNewLabel = new JLabel("请选择未缴纳的费用进行缴纳：");
		
		JScrollPane scrollPane = new JScrollPane();
		
		JButton btnNewButton = new JButton("确认支付");
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					PayActionPerformed();
				} catch (IOException ex) {
					throw new RuntimeException(ex);
				} catch (ClassNotFoundException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(35)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 303, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(28, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap(146, Short.MAX_VALUE)
					.addComponent(btnNewButton)
					.addGap(139))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnNewButton)
					.addContainerGap(28, Short.MAX_VALUE))
		);
		
		feetable = new JTable();
		feetable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u8D39\u7528\u91D1\u989D", "\u8D39\u7528\u7C7B\u578B", " \u7F34\u7EB3\u671F\u9650"
			}
		));
		scrollPane.setViewportView(feetable);
		setLayout(groupLayout);
		fillfeetable(MyAccount);
	}
	/**
	 * 初始化表格数据
	 */
	private void fillfeetable(Account mine) {
		DefaultTableModel dtm=(DefaultTableModel) feetable.getModel();
		dtm.setRowCount(0);//设为0行
		//连接数据库
			Vector v = new Vector();
			if(mine.getIFAccommodationFee()>0) {
				v.add(mine.getIFAccommodationFee());
				v.add("住宿费");
				v.add(mine.getAccomDeadline());
				dtm.addRow(v);
			}
		if(mine.getIFTution()>0) {
			v.add(mine.getIFTution());
			v.add("学费");
			v.add(mine.getTutionDeadline());
			dtm.addRow(v);
		}
		if(mine.getIFUtilityBill()>0) {
			v.add(mine.getIFUtilityBill());
			v.add("水电费");
			v.add(mine.getUtilityDeadline());
			dtm.addRow(v);
		}
	}
	/**
	 * 确认支付事件处理
	 *
	 */
	private void PayActionPerformed() throws IOException, ClassNotFoundException {
		int row=-1;
		row=this.feetable.getSelectedRow();
		String t= (String) feetable.getValueAt(row,1);
		if(row==-1){
			JOptionPane.showMessageDialog(null,"请选择缴费项目");
			return;
		}
		String type=MyAccount.toType(t);
		System.out.println("开始传输");
		//拉取选课队列
		Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
		//设置超时时间
		socket.setSoTimeout(3000);
		//构建IO
		OutputStream os = socket.getOutputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
		bw.write("64" + '\n'+studentId+'\n'+type+'\n');
		bw.flush();
		//从服务端输入到本地
		InputStream is = socket.getInputStream();
		BufferedReader br=new BufferedReader(new InputStreamReader(is));
		String str=br.readLine();
		System.out.println("传输结束");
		//输出输入流关闭
		is.close();
		os.close();
		socket.close();
		if (str.equals("true")) {JOptionPane.showMessageDialog(this, "缴费成功!");
			feetable.remove(row);
		} else {
			JOptionPane.showMessageDialog(this, "缴费失败!");
		}

	}


}
